package common;

/**
 * @author : wenchao.long
 * @date : Created in 2023-12-19 下午 5:22 周二
 * @description :
 */
public class ListNode {
	public int val;

	public ListNode next;

	public ListNode() {

	}

	public ListNode(int val) {
		this.val = val;
	}

	ListNode(int val, ListNode next) {
		this.val = val;
		this.next = next;
	}

	public static ListNode getNode(int[] arr) {
		if (arr.length == 0) {
			return null;
		}
		return getNode(new ListNode(), 0, arr);
	}

	private static ListNode getNode(ListNode node, int index, int[] arr) {
		if (index == arr.length) {
			return null;
		}
		node.val = arr[index];
		node.next = getNode(new ListNode(), index + 1, arr);
		return node;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		boolean flag = false;
		ListNode cur = this;
		while (cur != null) {
			if (flag) {
				sb.append("->");
			}
			sb.append(cur.val);
			flag = true;
			cur = cur.next;
		}
		return sb.toString();
	}

	public static void main(String[] args) {
		ListNode node = getNode(new int[]{1, 2, 3, 4, 5, 6});
		System.out.println(node);
	}
}
